/*
 * @Author: https://github.com/WangEn
 * @Author: https://gitee.com/lovetime/
 * @Date:   2020-07-29
 * @lastModify 2020-07-30 16:45:40
 * +----------------------------------------------------------------------
 * | Weadmin [ 后台管理模板 ]
 * | 基于Layui http://www.layui.com/
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'layer'], function (exports) {
  var $ = layui.jquery,
    layer = layui.layer;

  var baseApiUrl = 'https://www.fastmock.site/mock/aec70da5ba0d047d3929ec1bac48460f/admin';

  var request = {
    hello: function (str) {
      alert('Hello ' + (str || 'mymod'));
    },
    login: function (obj) {
      localStorage.setItem('username', res.data)
            layer.msg('登录成功，即将跳转~', function () {
              location.href = './index.html'
            });
      // $.ajax({
      //   url: baseApiUrl + '/user/login',
      //   type: 'POST',
      //   data: {
      //     username: obj.username,
      //     password: obj.password
      //   },
      //   success: function (res) {
      //     console.log('success'); //sy-log
      //     console.log(res); //sy-log
      //     if (res.code === 1) {
      //       console.log('登录成功'); //sy-log
      //       localStorage.setItem('username', res.data)
      //       layer.msg('登录成功，即将跳转~', function () {
      //         location.href = './index.html'
      //       });
      //     } else {
      //       layer.msg(res.msg, function () { });
      //     }
      //   }
      // })

    },
    getMenu: function () {
      var that = this;
      if (!$('#side-nav').length) {
        return;
      }
      // $.ajax({
      //   url: baseApiUrl + '/user/menu',
      //   type: 'POST',
      //   data: {
      //     username: localStorage.getItem('username'),
      //   },
      //   success: function(res) {
      //配置
      var res = {
        "code": 1,
        "msg": "ok",
        "data": {
          "menu": [

            {
              "id": 27, "name": "账号管理", "icon": "user", "url": "",
              "children": [
                { "id": 28, "name": "账号列表", "icon": "", "url": "./pages/id/id.html" },
                { "id": 29, "name": "备用列表", "icon": "", "url": "./pages/id/id_standby.html" },
                { "id": 30, "name": "验证码列表", "icon": "", "url": "./pages/id/code.html" },
                
              ]
            },
            {
              "id": 40, "name": "邀请码管理", "icon": "user", "url": "",
              "children": [
                { "id": 41, "name": "邀请码列表", "icon": "", "url": "./pages/id/bacInv.html" },
              ]
            },
            {
              "id": 50, "name": "新手和客服管理", "icon": "user", "url": "",
              "children": [
                { "id": 51, "name": "新手指南", "icon": "", "url": "./pages/id/nood.html" },
                { "id": 52, "name": "客服中心", "icon": "", "url": "./pages/id/service.html" },
                { "id": 53, "name": "反馈中心", "icon": "", "url": "./pages/id/return.html" },
              ]
            },
            // {
            //   "id": 1, "name": "会员管理6", "icon": "user", "url": "",
            //   "children": [
            //     { "id": 2, "name": "会员列表", "icon": "", "url": "./pages/member/list.html" },
            //     { "id": 3, "name": "会员删除", "icon": "", "url": "./pages/member/del.html" },
            //     {
            //       "id": 4, "name": "会员管理", "icon": "", "url": "",
            //       "children": [
            //         { "id": 5, "name": "输入框操作", "icon": "", "url": "./pages/member/addInput.html" },
            //         { "id": 6, "name": "三级菜单演示", "icon": "", "url": "./pages/404.html" },
            //         { "id": 7, "name": "导航菜单演示", "icon": "", "url": "./pages/404.html" }
            //       ]
            //     }
            //   ]
            // },
            // {
            //   "id": 8, "name": "文章管理", "icon": "read", "url": "",
            //   "children": [
            //     { "id": 9, "name": "文章列表", "icon": "", "url": "./pages/article/list.html" },
            //     { "id": 10, "name": "分类管理", "icon": "", "url": "./pages/article/category.html" }
            //   ]
            // },
            // {
            //   "id": 11, "name": "订单管理", "icon": "form", "url": "",
            //   "children": [
            //     { "id": 12, "name": "订单列表", "icon": "", "url": "./pages/order/list.html" }
            //   ]
            // },
            // {
            //   "id": 13, "name": "管理员权限", "icon": "util", "url": "",
            //   "children": [
            //     { "id": 14, "name": "管理员列表", "icon": "", "url": "./pages/admin/list.html" },
            //     { "id": 15, "name": "角色管理", "icon": "", "url": "./pages/admin/role.html" },
            //     { "id": 16, "name": "权限分类", "icon": "", "url": "./pages/admin/cate.html" },
            //     { "id": 17, "name": "权限管理", "icon": "", "url": "./pages/admin/rule.html" }
            //   ]
            // },
          ]
        }
      }

      if (res.code === 1) {
        var menuList = res.data.menu;
        var items = [];
        var _ul = $('<ul></ul>').addClass('nav').attr('id', 'nav');
        $.each(menuList, function (index, val) {
          var icon = val.icon ? '<i class="layui-icon layui-icon-' + val.icon + '"></i>' : '<i class="layui-icon layui-icon-app"></i>';
          var item = '<li id="menu' + val.id + '"><a _href="' + val.url + '">' + icon + '<cite>' + val.name +
            '</cite><i class="iconfont nav_right">&#xe697;</i></a></li>';

          items.push(item);
          if (val.children && val.children.length) {
            //有二级子菜单
            setTimeout(function () {
              that.getSubMenu(val.id, val.children);
            }, 500);
          }
        });
        $('#side-nav').empty();
        _ul.append(items.join(''));
        $('#side-nav').append(_ul);
      } else {
        layer.msg(res.msg, function () { });
      }
      //   }
      // })
    },
    /**
     * @param {Object} id 上级子菜单id,拼接menu获取元素
     * @param {Object} subList 子菜单列表
     */
    getSubMenu: function (id, subList) {
      var that = this;
      var subItems = [];
      var subUl = $('<ul></ul>').addClass('sub-menu');
      $.each(subList, function (idx, sub) {
        var subItem = '<li id="menu' + sub.id + '"><a _href="' + sub.url + '"><i class="iconfont">&#xe6a7;</i><cite>' +
          sub.name + '</cite></a></li>';
        subItems.push(subItem);
        if (sub.children && sub.children.length) {
          setTimeout(function () {
            that.getSubMenu(sub.id, sub.children);
          }, 1500);
        }
      });
      subUl.append(subItems.join(''));
      $('#menu' + id).append(subUl);
    }
  }
  exports('http', request);

});
